package com.cn.demo.springboot.quartz.config;

import java.util.Properties;

import org.quartz.JobListener;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 使用spring管理Scheduler
 *  
 *  流程 1、初始化调度器
 *  	2、初始化任务
 *  	3、启动任务
 * 
 * 
 * @author admin
 *
 */
@Configuration
public class QuartzConfig {
	private final static Logger log = LoggerFactory.getLogger(QuartzConfig.class);
	/**
	 * 线程池 线程数
	 */
	@Value(value = "${org.quartz.threadPool.threadCount}")
	private String threadCount;
	
	/**
	 * tj
	 * 2018年11月30日 下午1:56:16
	 */
	@Bean
	public JobListener getJobListener() {
		return new JobListenerImpl();
	}

	@Bean(name = "scheduler")
	public Scheduler getScheduler(JobListener jobListener) {
		try {
			StdSchedulerFactory sf = new StdSchedulerFactory();
			Properties props = new Properties();
			props.put("org.quartz.threadPool.threadCount", threadCount);// 线程数
			sf.initialize(props);
			Scheduler scheduler = sf.getScheduler();
			scheduler.getListenerManager().addJobListener(jobListener);
			return scheduler;
		} catch (SchedulerException e) {
			log.error("Scheduler 初始化失败!");
			e.printStackTrace();
		}
		return null;
	}

}
